package com.zdglf.mmd.utils;

import android.content.Context;
import android.graphics.Bitmap;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by zhangmike on 1/1/17.
 */
public class FileUtils {
    public static boolean saveBitmap(Bitmap bmp, String filePath){
        if(bmp == null){
            return false;
        }
        File f = new File(filePath);
        FileOutputStream fos = null;
        try{
            f.delete();
            f.createNewFile();
            fos = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally{
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    static public void copyToFath(InputStream is , String path){
        try{
            File f = new File(path);
            f.deleteOnExit();
            f.createNewFile();
            FileOutputStream fos = new  FileOutputStream(f);
            int i = 0;
            byte[] buf = new byte[1024];
            while((i = is.read(buf))>0){
                fos.write(buf, 0, i);
            }
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    static public List<File> getDirectoryFileList(final File f, final String filters){
        List<File> fileList = new ArrayList<File>();
        if(!f.isDirectory()){
            return fileList;
        }
        File[] files = f.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.getName().startsWith(".")){
                    return false;
                }
                if(filters == null||pathname.isDirectory()){
                    return true;
                }else{
                    String[] filterData = filters.split("\\|");
                    for(String f:filterData){
                        if(pathname.getName().endsWith(f.toLowerCase())|| pathname.getName().endsWith(f.toUpperCase())){
                            return true;
                        }

                    }
                    return false;
                }

            }
        });
        fileList.addAll(Arrays.asList(files));
        return fileList;
    }
    static public String readRawTextFile(Context c, int resId) {
        InputStream inputStream = c.getResources().openRawResource(resId);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
